package com.youdianstar.app.ui.op

import android.app.Activity
import androidx.recyclerview.widget.GridLayoutManager
import com.youdianstar.app.common.PHOTO_ALBUM
import com.youdianstar.app.common.PHOTO_CAMERA
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.entity.FileUpResult
import com.youdianstar.app.common.network.BaseApiResultObserver
import com.youdianstar.app.common.util.LogUtil
import com.youdianstar.app.common.widget.adapter.GridItemDecoration
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.adapter.ShopImgEdtAdapter
import com.youdianstar.app.databinding.OpShopImagesBinding
import com.youdianstar.app.util.AppDialogUtil
import com.youdianstar.app.util.DataAppManager
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: ShopImageActivity
 * Author: 82149 Create: 2023/8/3 18:36
 * Changes (from 2023/8/3)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class ShopImageActivity :BaseActivity<OpShopImagesBinding,OPMerchantViewModel>(){
    override fun layoutId(): Int = R.layout.op_shop_images

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): OPMerchantViewModel {
        val model: OPMerchantViewModel by viewModel()
        return model
    }

    //编辑模式
    private var isEdtModel = false
    //图片选择
    private var selectType: Int = 0
    private var picAdapter: ShopImgEdtAdapter?=null

    override fun initData() {
        super.initData()
        mBinding.headBar.initTitle(this,getString(R.string.shop_shopPhoto), backAction = {
            setResultBack()
        })

        mBinding.rcyList.layoutManager = GridLayoutManager(this,3)
        mBinding.rcyList.addItemDecoration(GridItemDecoration.Builder(this).color(R.color.transparent).size(10).build())
        picAdapter = ShopImgEdtAdapter(this, mutableListOf(),object :ShopImgEdtAdapter.OnPicSelect{
            override fun onAdd() {
                showPhotoDialog()
            }

            override fun onDelete(index: Int) {
                if (DataAppManager.instance.imageList.isEmpty().not() && index <= DataAppManager.instance.imageList.size - 1) {
                    mViewModel.deleteImage(DataAppManager.instance.imageList[index])
                    DataAppManager.instance.imageList.removeAt(index)
                    picAdapter?.refreshData(DataAppManager.instance.imageList,true)
                }
            }

            override fun onItemClick(url: String) {
                AppDialogUtil.instance.showImageCheckDialog(this@ShopImageActivity, url)
            }
        })
        mBinding.rcyList.adapter = picAdapter

        setEditLayout(true)
    }

    override fun initClickEvent() {
        mViewModel.fileDeleteLiveData.observe(this, object : BaseApiResultObserver<Any>(true) {
            override fun onDataResult(data: Any?) {
                super.onDataResult(data)
            }
        })

        mViewModel.fileUpLoadLiveData.observe(this,object : BaseApiResultObserver<FileUpResult>(false){
            override fun onDataResult(data: FileUpResult?) {
                super.onDataResult(data)
                dismissDialog()
                data?.let {
                    LogUtil.i("----add ="+it.value)
                    DataAppManager.instance.imageList.add(it.value)
                    picAdapter?.refreshData(DataAppManager.instance.imageList,true)
                }
            }

            override fun onNetError(code: Int, e: String?) {
                super.onNetError(code, e)
                dismissDialog()
                toastShow(e)
            }
        })
    }

    private fun setEditLayout(isEdit :Boolean){
        isEdtModel = isEdit
//        mBinding.tvEdit.text = if (isEdit) getString(R.string.Public_save) else getString(R.string.Profile_edit)
        //设置图片数据
        picAdapter?.refreshData(DataAppManager.instance.imageList,isEdit)
    }

    private fun setResultBack(){
        //DataAppManager.instance.imageList = images
        setResult(Activity.RESULT_OK)
    }

    override fun onBackPressed() {
        setResultBack()
        super.onBackPressed()
    }

    /****************************图片选择部分*******************************/

    /**图片选择器*/
    private fun showPhotoDialog() {
        AppDialogUtil.instance.showSelectDialog(this) { selectIndex ->
            selectType = selectIndex
            when (selectIndex) {
                PHOTO_CAMERA -> requestCameraPermission {
                    showPhotoPicker(PHOTO_CAMERA)
                }
                PHOTO_ALBUM -> requestAlbumPermission {
                    showPhotoPicker(PHOTO_ALBUM)
                }
            }
        }
    }

    /**
     * 调用-ImagePicker
     * @param type
     */
    private fun showPhotoPicker(type: Int) {
        startSLImagePicker(this, type = type, needCrop = true) { path ->
            mViewModel.upLoadImage(path)
        }
    }

    override fun onPermissionsGranted(requestCode: Int, perms: List<String>) {
        super.onPermissionsGranted(requestCode, perms)
        if (requestCode == REQUEST_PERMISSION_CODE)
            showPhotoPicker(selectType)
    }
}